﻿using System;
using WiMo.Games;
using System.Drawing;
using WiMo.Games.Drawables;
using wimo = WiMo.Games.Drawables;

namespace WiMo.Games.WM5.Drawables
{
    public class SpriteImage : SpriteImageBase
    {

        public SpriteImage()
        {
            IsVisible = true;
            Origin = Origin.Center;
            Force = Vector2.Zero;
            ColorKey = Color.Transparent;
            IsStatic = false;
            Position = PointF.Origin;
            Mass = 1;
            Torque = 0.0f;
            ImageIndex = 0;
            CollisionShape = CollisionShapes.Rectangle;
        }

        public override CollisionShapes CollisionShape
        {
            get
            {
                return base.CollisionShape;
            }
            set
            {
                CollisionDetectionPoints.Clear();

                if (TextureList != null &&
                    TextureList.Count > 0
                    && value == CollisionShapes.Sphere)
                {
                    var rads = 2.0f * Math.PI / 8.0f;
                    var radius = Size.Width / 2;

                    for (var idx = 0.0f; idx < 8f; ++idx)
                        CollisionDetectionPoints.Add(new Location(Math.Sin(rads * idx) * radius, Math.Cos(rads * idx) * radius));
                }

                base.CollisionShape = value;
            }
        }

        public override int ImageIndex
        {
            get { return base.ImageIndex; }
            set
            {
                Invalidate();

                //So we can refresh based upon the new image.
                var disposable = DrawInformation as IDisposable;
                if (disposable != null)
                {
                    disposable.Dispose();
                    DrawInformation = null;
                }

                CollisionDetectionPoints.Clear();

                //The sprite might be a different size.
                if (TextureList.Count > 0 && CollisionShape == CollisionShapes.Sphere)
                {
                    var rads = 2.0f * Math.PI / 8.0f;
                    var radius = Size.Width / 2;

                    for (var idx = 0.0f; idx < 8f; ++idx)
                        CollisionDetectionPoints.Add(new Location(Math.Sin(rads * idx) * radius, Math.Cos(rads * idx) * radius));
                }

                base.ImageIndex = value;
            }
        }

        public override void Load(IContentManager contentManager, string name)
        {
            var bitMap = contentManager.GetTexture(name);

            TextureList.Add(bitMap);

            CollisionDetectionPoints.Clear();

            if (CollisionShape == CollisionShapes.Sphere)
            {
                var rads = 2.0f * Math.PI / 8.0f;
                var radius = Size.Width / 2;

                for (var idx = 0.0f; idx < 8f; ++idx)
                    CollisionDetectionPoints.Add(new Location(Math.Sin(rads * idx) * radius, Math.Cos(rads * idx) * radius));
            }
        }

        public override int AddImage(IContentManager contentManager, string name)
        {
            var bitmap = contentManager.GetTexture(name);

            TextureList.Add(bitmap);

            return TextureList.Count - 1;
        }

        private float _rotation = 0.0f;
        public override float Rotation
        {
            get { return _rotation; }
            set
            {
                if (_rotation != value)
                {
                    //TextureList[ImageIndex].TextureInformation = OpenNETCF.Drawing.Imaging.ImageUtils.Rotate(TextureList[ImageIndex].TextureInformation as Bitmap, _rotation);
                    //OpenNETCF.Drawing.RotationAngle rot;
                    //_rotation = value;

                    //var degs = (int)MathHelper.ToDegrees(_rotation);
                    //switch (degs)
                    //{
                    //    case 0: rot = OpenNETCF.Drawing.RotationAngle.Zero; break;
                    //    case 90: rot = OpenNETCF.Drawing.RotationAngle.Clockwise90; break;
                    //    case 180: rot = OpenNETCF.Drawing.RotationAngle.Clockwise180; break;
                    //    case 270: rot = OpenNETCF.Drawing.RotationAngle.Clockwise270; break;
                    //    default:
                    //        throw new Exception("Can only rotate images 0, 90, 180, 270.  I know this sucks, but it's currently a limitation on the device.  It's in our plan to change this.");
                    //}
                    //TextureList[ImageIndex].TextureInformation = OpenNETCF.Drawing.Imaging.ImageUtils.Rotate(Texture.TextureInformation as Bitmap, rot);
                    DrawInformation = null;
                }

            }
        }

        public ITexture Texture
        {
            get { return TextureList[ImageIndex]; }
        }

        public override Size Size
        {
            get
            {
                var bitmap = Texture.TextureInformation as Bitmap;
                return new Size(bitmap.Width, bitmap.Height);
            }
            set { throw new Exception("Setting size of bitmap not supported."); }
        }

        public override Size UnscaledSize
        {
            get
            {
                return new Size((int)(Size.Width / Scale.X), (int)(Size.Height / Scale.Y));
            }
            set { throw new Exception("Setting size of bitmap not supported."); }
        }

        public override void Render(IDisplay display)
        {
            display.Render(this);
        }

        public override void Dispose()
        {
            throw new NotImplementedException();
        }
    }
}
